// product-detail.ts
Component({
  data: {
    productId: 0,
    productName: '',
    productPrice: 0,
    productSales: 0,
    product: null
  },

  methods: {
    onLoad(options: any) {
      const productId = parseInt(options.id || '0')
      this.setData({ productId })
      this.loadProductDetail()
    },

    loadProductDetail() {
      // 模拟产品数据
      const mockProduct = {
        id: this.data.productId,
        name: '深层清洁面膜',
        price: 128,
        sales: 256,
        image: '/images/product1.jpg',
        description: '采用天然植物精华，深层清洁毛孔，去除黑头粉刺，让肌肤重现光彩。适合各种肤质使用。',
        specs: {
          brand: '美丽优选',
          size: '50ml',
          shelfLife: '3年',
          ingredients: '天然植物精华、活性炭、透明质酸'
        }
      }

      this.setData({
        product: mockProduct,
        productName: mockProduct.name,
        productPrice: mockProduct.price,
        productSales: mockProduct.sales
      })
    },

    addToCart() {
      const { product } = this.data
      
      if (!product) return

      const cart = wx.getStorageSync('cart') || []
      const existingItem = cart.find((item: any) => item.id === product.id)

      if (existingItem) {
        existingItem.quantity += 1
      } else {
        cart.push({
          ...product,
          quantity: 1
        })
      }

      wx.setStorageSync('cart', cart)
      
      wx.showToast({
        title: '已加入购物车',
        icon: 'success',
        duration: 1500
      })
    }
  }
})